/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pass.jtorrenteditor.list.model;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import za.co.pass.jtorrenteditor.helper.Utils;

/**
 *
 * @author alabuschagne
 */
public class FileListModel implements ListModel
{
    private List files = new LinkedList();

    public int getSize()
    {
        return getFiles().size();
    }

    private void buildString(StringBuilder sb, List list)
    {
        Iterator it = list.iterator();
        sb.append(":");
        while(it.hasNext())
        {
            Object o = it.next();
            if(o instanceof short[])
            {
                sb.append(new String(Utils.short2Char((short[]) o)));
            }
            else if(o instanceof String)
            {
                sb.append((String) o);
            }
            else if(o instanceof Integer)
            {
                sb.append(((Integer) o).toString());
            }
            else if(o instanceof List)
            {
                buildString(sb, (List) o);
            }
            else if(o instanceof Map)
            {
                buildString(sb, (Map) o);
            }
        }
        sb.append(":");
    }

    private void buildString(StringBuilder sb, Map map)
    {
        Iterator<String> it = map.keySet().iterator();
        while(it.hasNext())
        {
            String key = it.next();
            sb.append(" [");
            sb.append(key);
            sb.append("] ");

            boolean fileHash = (key.compareToIgnoreCase("filehash") == 0)
                                || (key.compareToIgnoreCase("ed2k") == 0);
            
            if(map.get(key) instanceof short[])
            {
                if(fileHash)
                {
                    short[] b = (short[]) map.get(key);
                    StringBuilder sbfh = new StringBuilder();
                    for(int i = 0; i < b.length; i++)
                    {
                        String str = Integer.toHexString(b[i]);
                        str = Utils.addLeadingZeros(str, 2);
                        str = str.toUpperCase();
                        sbfh.append(str);
                        if(i < b.length - 1)
                        {
                            sbfh.append(" ");
                        }
                    }
                    sb.append(sbfh);
                }
                else
                {
                    sb.append(new String(Utils.short2Char((short[]) map.get(key))));
                }
            }
            else if(map.get(key) instanceof String)
            {
                sb.append((String) map.get(key));
            }
            else if(map.get(key) instanceof Integer)
            {
                sb.append(((Integer) map.get(key)).toString());
            }
            else if(map.get(key) instanceof List)
            {
                buildString(sb, (List) map.get(key));
            }
            else if(map.get(key) instanceof Map)
            {
                buildString(sb, (Map) map.get(key));
            }
            sb.append("|");
        }
    }

    public Object getElementAt(int index)
    {
        Map map = (Map) getFiles().get(index);
        StringBuilder sb = new StringBuilder();
        buildString(sb, map);
        return sb.toString();
    }

    public void addListDataListener(ListDataListener l)
    {
    }

    public void removeListDataListener(ListDataListener l)
    {
    }

    /**
     * @return the files
     */
    public List getFiles()
    {
        return files;
    }

    /**
     * @param files the files to set
     */
    public void setFiles(List files)
    {
        this.files = files;
    }
}
